<?php
/**
 * ClassName:运费价格处理器
 * @auditor:leijunxiang
 * @time 2025/4/15 14:45
 */

namespace app\tool\freight;

use app\model\freight\FreightPrice;
use app\tool\ToolBase;

class FreightPriceHandle extends ToolBase
{
    //渠道详情对象数组
    protected $channelDetailAttr;
    //数量
    protected $count;
    //运费价格对象数组
    protected $freightPriceAttr;

    /**
     * (运费价格数组)开始处理(渠道详情对象数组,数量,当前重量)
     */
    public function startHandle($channelDetailAttr, $count, $currentWeight)
    {
        if (empty($channelDetailAttr) || empty($count) || empty($currentWeight)) {
            $this->errCode = FAIL;
            $this->errMsg = '参数错误';
            return [];
        }
        $arr = [];
        //初始化数据
        $this->initData($channelDetailAttr, $count);
        foreach ($this->channelDetailAttr as $channelDetail){
            //(重量数组)生成该渠道全部重量数组(渠道详情)
            $weightAttr = $this->createChannelAllWeightAttr($channelDetail);
            //(选择的重量)选取对应重量(全部重量数组,当前重量)
            $selectWeight = $this->selectWeight($weightAttr, $currentWeight);
            //生成运费价格对象数组(选择的重量,当前重量,渠道详情对象)
            $this->createFreightPriceAttr($selectWeight, $currentWeight, $channelDetail);
        }
        $arr = $this->freightPriceAttr;
        //清楚数据
        $this->cleanData();
        return $arr;
    }

    /**
     * 初始化数据
     */
    protected function initData($channelDetailAttr, $count)
    {
        //渠道详情对象数组
        $this->channelDetailAttr = $channelDetailAttr;
        //数量
        $this->count = $count;
        //运费价格对象数组
        $this->freightPriceAttr = [];
    }

    /**
     * (重量数组)生成该渠道全部重量数组(渠道详情)
     */
    protected function createChannelAllWeightAttr($channelDetail)
    {
        $arr = [];
            $priceAttr = $channelDetail->getWarehousePrice();
            foreach ($priceAttr as $item) {
                $weightName = $item['weightName'];
                //目前只取KG单位的
                if (strpos($weightName, 'KG') == false) {
                    continue;
                }
                $weight = $item['weight'];
                if (in_array($weight, $arr)) {
                    continue;
                }
                $arr[] = $weight;
            }

        return $arr;
    }

    /**
     * (选择的重量)选取对应重量(全部重量数组,当前重量)
     */
    protected function selectWeight($weightAttr, $currentWeight)
    {
        $obj = app(WeightSelector::class);
        //(选择重量)开始处理(重量数组,当前重量)
        $weight = $obj->startHandle($weightAttr, $currentWeight);
        return $weight;
    }

    /**
     * 生成运费价格对象数组(选择的重量,当前重量)
     */
    protected function createFreightPriceAttr($selectWeight, $currentWeight,$channelDetail)
    {
            $priceAttr = $channelDetail->getWarehousePrice();
            foreach ($priceAttr as $item) {
                $weight = $item['weight'];
                if ($weight != $selectWeight||$item['price']==0) {
                    continue;
                }
                $arr = [];
                //货代id
                $forwardId = $channelDetail->getForwardId();
                $arr['forwardId'] = $forwardId;
                //货代名称
                $name = $channelDetail->getName();
                $arr['name'] = $name;
                //渠道id
                $channelId = $channelDetail->getChannelId();
                $arr['channelId'] = $channelId;
                //渠道名称
                $channelName = $channelDetail->getChannelName();
                $arr['channelName'] = $channelName;
                //仓库id
                $warehouseId = $item['warehouseId'];
                $arr['warehouseId'] = $warehouseId;
                //详情id
                $detailId = $item['detailId'];
                $arr['detailId'] = $detailId;
                //区域名称
                $areaName = $channelDetail->getAreaName();
                $arr['areaName'] = $areaName;
                //重量
                $arr['weight'] = $weight;
                //单价
                $price = $item['price'];
                $arr['price'] = $price;
                //数量
                $number = $this->count;
                $arr['number'] = $number;
                //运费价格
                $freightPrice = $price * $number * $currentWeight;
                // 修改：保留一位小数
                $arr['freightPrice'] = round($freightPrice, 1);
                $this->freightPriceAttr[] = $arr;
            }

    }

    /**
     * 清楚数据
     */
    protected function cleanData()
    {
        //渠道详情对象数组
        $this->channelDetailAttr = [];
        //数量
        $this->count = 0;
        //运费价格对象数组
        $this->freightPriceAttr = [];
    }


}